<template>
  <div class="order">
    <el-dialog
      title="修改地址"
      :visible.sync="dialogVisibe"
      width="30%"
      @close="close">
      <el-form :model="form" ref="form" :rules="rules" label-width="80px" :inline="false" size="normal">
        <el-form-item label="省县市" prop="address">
          <el-cascader :options="cityDate" v-model="form.address" clearable>
          </el-cascader>
        </el-form-item>
        <el-form-item label="具体地址" prop="detailAddress">
          <el-input v-model="form.detailAddress"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="editAddress">确定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="物流查询"
      :visible.sync="dialogVisibeShow"
      width="40%"
      @close="dialogVisibeShow=false">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>

    <Breadcrumb/>
    <el-card shadow="hover">
      <el-input class="search" v-model="queryObj.query" placeholder="请输入内容" @clear="clear" :clearable="true" @keyup.enter.native="search">
        <el-button slot="append" icon="el-icon-search" size="default" @click="search"></el-button>
      </el-input>
      <Table
        :data="data"
        :columns="columns"
      >
        <template #date>
          <el-table-column label="下单时间">
            <template slot-scope="scope">
              <div>
                {{scope.row.create_time|dateFormat}}
              </div>
            </template>
          </el-table-column>
        </template>
        <template #isPay>
          <el-table-column
            label="是否付款"
            width="120px"
          >
            <template slot-scope="scope">
              <div>
                <el-tag v-if="scope.row.pay_status==='0'"  type="danger">未付款</el-tag>
                <el-tag v-else  type="success">已付款</el-tag>
              </div>
            </template>
          </el-table-column>
        </template>
        <template #operate>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div>
                <el-button type="primary" size="mini" @click="openDialogEdit(scope.row)" icon="el-icon-edit"></el-button>
                <el-button type="success" size="mini" @click="showProgress(scope.row)" icon="el-icon-location-outline"></el-button>
              </div>
            </template>
          </el-table-column>
        </template>
      </Table>
      <Pagination
        :pagenum="queryObj.pagenum"
        :pagesize="queryObj.pagesize"
        :total="total"
        :pagesizes="pagesizes"
        :handleSizeChange="handleSizeChange"
        :handleCurrentChange="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import cityDate from './citydata.js'
export default {
  name: 'order',
  data () {
    return {
      data: [],
      columns: [
        {
          prop: 'order_number',
          label: '订单编号',
          width: '400px'
        },
        {
          prop: 'order_price',
          label: '订单价格',
          width: '100px'
        },
        {
          prop: 'is_send',
          label: '是否发货',
          width: '100px'
        }
      ],
      queryObj: {
        query: '',
        pagenum: 1,
        pagesize: 4
      },
      pagesizes: [1, 2, 3, 4, 5, 6, 7],
      total: 0,
      cityDate,
      dialogVisibe: false,
      form: {
        address: [],
        detailAddress: ''
      },
      rules: {
        address: [
          { required: true, type: 'array', message: '请选择地区', trriger: 'blur' }
        ],
        detailAddress: [
          { required: true, message: '请填写具体地址', trriger: 'blur' }
        ]
      },
      dialogVisibeShow: false,
      orderId: 0,
      activities: []
    }
  },
  methods: {
    async getOrderList () {
      const { data: res } = await this.$http.get('orders', { params: this.queryObj })
      if (res.meta.status === 200) {
        this.data = res.data.goods
        this.total = res.data.total
      }
    },
    search () {
      this.getOrderList()
    },
    clear () {
      this.queryObj.query = ''
    },
    openDialogEdit () {
      this.dialogVisibe = true
    },
    handleSizeChange (val) {
      this.queryObj.pagesize = val
      this.getOrderList()
    },
    handleCurrentChange (val) {
      this.queryObj.pagenum = val
      this.getOrderList()
    },
    editAddress () {
      this.$refs.form.validate(callback => {
        if (callback) {
          this.$message({
            type: 'success',
            message: '修改成功',
            duration: 1000
          })
          this.dialogVisibe = false
        } else {
          this.$message({
            type: 'error',
            message: '请填写必要信息',
            duration: 1000
          })
        }
      })
    },
    cancel () {
      this.$refs.form.resetFields()
    },
    close () {
      this.dialogVisibe = false
      this.$refs.form.resetFields()
    },
    showProgress (row) {
      this.orderId = row.order_id
      this.dialogVisibeShow = true
      this.getKuaidiList()
    },
    async getKuaidiList () {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status === 200) {
        this.activities = res.data
      }
    }
  },
  created () {
    this.getOrderList()
    this.cityDate = cityDate
  }
}
</script>

<style lang="less" scoped>
.search{
  width: 300px;
}
</style>
